package com.gugee.ins.data.task.monitor.service;

import com.gugee.common.task.dto.TaskParam;
import com.gugee.ins.common.dto.RankTaskParam;
import com.gugee.ins.data.task.config.TaskConfig;
import com.xiguaji.boot.toolkit.JsonUtil;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.log.XxlJobLogger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;


/**
 * 处理定时监控任务
 * @author XiGua
 */
@Slf4j
public abstract class AbstractMonitorProducerHandler {

    /**
     *
     */
    @Autowired
    public TaskConfig taskConfig;

    /**
     *
     * @param name
     * @param param
     * @return
     */
    public ReturnT<String> xxlJobStart(String name, String param){
        long start = System.currentTimeMillis();
        try {
            log.info(name + " 开始");
            XxlJobLogger.log(name + " 开始");
            TaskParam taskParam = null;
            if(!StringUtils.isEmpty(param)){
                taskParam = JsonUtil.jsonParse(TaskParam.class, param);
                if(taskParam == null){
                    log.info(name+ " 参数错误,param：{}",param);
                    XxlJobLogger.log(name+ " 参数错误,param：{}",param);
                    return ReturnT.FAIL;
                }
            }

            if(taskParam == null){
                taskParam = new RankTaskParam();
            }

            handle(taskParam);
            return ReturnT.SUCCESS;
        } catch (Exception ex) {
            XxlJobLogger.log(ex);
            ex.printStackTrace();
            return ReturnT.FAIL;
        }finally {
            log.info(name + " 结束, cost total={}", (System.currentTimeMillis() - start) / 1000);
            XxlJobLogger.log(name + " 结束, cost total={}",(System.currentTimeMillis() - start) / 1000);
        }
    }

    /**
     * 处理定时监控任务
     * @param taskParam
     */
    public abstract void handle(TaskParam taskParam);
}
